#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2017-08-15 09:34:17
# Project: bilibili
# 36379

### b站 id 6


from pyspider.libs.base_handler import *
import time
import config as cf
import pymysql
import redis
import db_action


class Handler(BaseHandler):
    crawl_config = {

    }

    def __init__(self):
        conn = pymysql.connect(**cf.mysql_option)
        cursor = conn.cursor()
        r = redis.StrictRedis(**cf.redis)
        self.conn = conn
        self.cursor = cursor
        self.r = r

    @every(minutes=4 * 60)
    def on_start(self):
        self.cursor.execute(
            "select account_id,account_name,plat_name,user_id,get_data_url from qp.med_plat_account WHERE plat_id = 2;")
        result = self.cursor.fetchall()
        pagesize = 30
        for i in result:
            mid = i["get_data_url"].split("?").pop().split("=").pop()
            self.crawl('http://space.bilibili.com/ajax/member/getSubmitVideos', callback=self.page_detail,
                       validate_cert=False,
                       params={
                           "mid": mid,
                           "page": 1,
                           "pagesize": pagesize
                       },
                       save={
                           "user_info": i,
                           "third_id": mid,
                           "page": 0,
                           "pagesize": pagesize}
                       )

    @config(age=10 * 24 * 60 * 60)
    def page_detail(self, response):
        user_info = response.save["user_info"]
        third_id = response.save["third_id"]
        page = response.save["page"]
        pagesize = response.save["pagesize"]
        mid = third_id
        if response.ok is True:
            # total count
            pages = response.json['data']['pages']
            # deal with the data
            rs = {}
            for record in response.json["data"]["vlist"]:
                rs['title_name'] = record['description']
                rs['flow_count'] = record['play']
                time_local = time.localtime(int(record['created']))
                rs['add_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
                rs['url'] = 'https://www.bilibili.com/video/av%s/' % (record['aid'])
                rs['plat_id'] = 2
                rs['third_id'] = third_id
                rs['account_id'] = user_info['account_id']
                rs['account_name'] = user_info['account_name']
                rs['plat_name'] = user_info['plat_name']
                rs['user_id'] = user_info['user_id']
                rs['tag'] = 0
                rs['adv_id'] = 0
                rs['audit_status'] = 1

                db_action.store_info_bilibili(rs)
                # crawl the next page
                if (page < pages):
                    page += 1
                    self.crawl('http://space.bilibili.com/ajax/member/getSubmitVideos', callback=self.page_detail,
                               validate_cert=False,
                               params={
                                   "mid": mid,
                                   "page": page,
                                   "pagesize": pagesize
                               },
                               save={
                                   "user_info": user_info,
                                   "third_id": mid,
                                   "page": page,
                                   "pagesize": pagesize}
                               )
                else:
                    break;

        else:
            return False
